<?php

namespace app\index\service\ranks;

use app\common\model\Member;
use app\index\dao\LawyerRankDao;

/**
 * 律师专业排行榜
 * Class ProfessionalRank
 * @package app\index\service\ranks
 */
class CommentRank implements RankInterface
{
    private LawyerRankDao $lawyerRankDao;

    public function __construct(LawyerRankDao $lawyerRankDao)
    {

        $this->lawyerRankDao = $lawyerRankDao;
    }

    public function rank()
    {
        $tableName = Member::getTable();
        Member::leftJoin('member_info mi', "$tableName.id=mi.memberId")
            ->where("$tableName.type", '1')
            ->where("$tableName.subType", '0')
            ->where("$tableName.status", '1')
            ->tableField(['id',], $tableName)
            ->tableField(['answerNumber', 'commentLevel'], 'mi')
            ->order('mi.commentLevel', 'DESC')
            ->order('mi.answerNumber', 'DESC')
            ->order('id', 'ASC')
            ->select()
            ->each(function (Member $member, $index) {
                $params = [
                    'rankType' => 'comment',
                    'rank' => $index + 1,
                    'lawyerId' => $member->id,
                    'answerNumber' => $member->answerNumber,
                    'commentLevel' => $member->commentLevel,
                ];
                $this->lawyerRankDao->replaceActive($params);
            });
    }
}
